﻿package base.support;

import java.util.ArrayList;
import java.util.List;

public class Paged<T>
{
	private static final int DEFAULT_PAGE_SIZE = 20;	//默认每页记录数
	private List<T> data;	//当前的数据
	private int totalCount;	//总记录数
	private int firstResult;	//当前第一条数据的位置
	private int maxResults = DEFAULT_PAGE_SIZE;	//每页记录数
	
	public Paged()
	{
		this(new ArrayList<T>(), 0, 0, DEFAULT_PAGE_SIZE);
	}
	
	public Paged(List<T> data, int totalCount, int firstResult, int maxResults)
	{
		this.data = data;
		this.totalCount = totalCount;
		this.firstResult = firstResult;
		this.maxResults = maxResults;
	}

	public int getFirstResult()
	{
		return firstResult;
	}

	public void setFirstResult(int firstResult)
	{
		this.firstResult = firstResult;
	}

	public int getMaxResults()
	{
		return maxResults;
	}

	public void setMaxResults(int maxResults)
	{
		this.maxResults = maxResults;
	}

	public int getTotalCount()
	{
		return totalCount;
	}

	public void setTotalCount(int totalCount)
	{
		this.totalCount = totalCount;
	}

	public List<T> getData()
	{
		return data;
	}
	
	public void setData(List<T> data)
	{
		this.data = data;
	}
	
	/**
	 * 取总页数。
	 */
	public long getTotalPageCount()
	{
		if (totalCount % maxResults == 0)
			return totalCount / maxResults;
		else
			return totalCount / maxResults + 1;
	}
	
	/**
	 * 取当前页码，页码从1开始。
	 */
	public long getCurrentPageNo()
	{
		return firstResult / maxResults + 1;
	}
	
	/**
	 * 该页是否有上一页。
	 */
	public boolean hasPreviousPage()
	{
		return this.getCurrentPageNo() > 1;
	}
	
	/**
	 * 该页是否有下一页。
	 */
	public boolean hasNextPage()
	{
		return this.getCurrentPageNo() < this.getTotalPageCount() - 1;
	}
}
